#!/bin/bash

readonly API2ROOT=/data/wwwroot/dtrd
readonly SERVER_NAME=api
readonly SERVER_JAR=dtrd-boot.jar

readonly MAX_XMS=256m
readonly MAX_XMX=256m
readonly MAX_METASPACE=256m

function killProcess(){
    # shellcheck disable=SC2009
    ID=$(ps -ef | grep java | grep $SERVER_JAR | awk '{print $2}')
    if [ -z "$ID" ];
    then
      echo "$SERVER_JAR not found sleep check all java processes and sleep for 5 seconds"
      # choice run options
      while :; do echo
	      echo "未找到 $SERVER_JAR 系统JAVAV进程如下 是否继续 y/n"
        echo ''
        ps -ef | grep java 
        echo ''
        read choice
        choice=$(echo $choice | tr [a-z] [A-Z])
        if [ "$choice"x = "Y"x ]; then
          echo " 继续执行剩余流程 "
          echo '======================================================'
          echo ''
          break
        else
          echo " 结束运行 " 
          echo '======================================================'
          echo ''
          return 1
        fi    
      done
    else
      echo "--------------- ready to kill $ID"
      # shellcheck disable=SC2009
      ps -ef | grep "$ID"
      sleep 2

      for id in $ID
      do
      echo "kill child process $id in $ID"
      kill -9 "$id"
      done
      echo "---------------kill $ID success"
    fi  
     return 0
}

function runcrm(){
  cd "$API2ROOT"/$SERVER_NAME
  mv $API2ROOT/$SERVER_NAME/$SERVER_NAME.log logs/$SERVER_NAME$(date +%Y%m%d%H%M).log
  echo "current path `pwd`"
  echo "ready to start $SERVER_JAR"
  nohup java -jar -Xms$MAX_XMS -Xmx$MAX_XMX -XX:MetaspaceSize=$MAX_METASPACE -XX:MaxMetaspaceSize=$MAX_METASPACE  $API2ROOT/$SERVER_NAME/$SERVER_JAR > $API2ROOT/$SERVER_NAME/$SERVER_NAME.log 2>&1 &
  echo "after start $SERVER_JAR "
  ps -ef | grep java | grep $SERVER_JAR
  # shellcheck disable=SC2046
  tail -f "$API2ROOT"/$SERVER_NAME/$SERVER_NAME.log -n 200
}

function replaceJar(){
    serverJar=/tmp/$SERVER_JAR

    if [ -f "$serverJar" ]; then
      # shellcheck disable=SC2012
      ftime=$(ls --full-time $API2ROOT/$SERVER_NAME/$SERVER_JAR | awk '{print $6$7}')
      echo "$ftime"
      cp "$API2ROOT"/$SERVER_NAME/$SERVER_JAR ~/history/api2/$SERVER_NAME/$SERVER_NAME"$ftime".jar
      echo " cp $API2ROOT/$SERVER_NAME/$SERVER_JAR ~/history/api2/$SERVER_NAME/$SERVER_NAME$ftime.jar"
      mv "$API2ROOT"/$SERVER_NAME/$SERVER_JAR "$API2ROOT"/$SERVER_NAME/$SERVER_JAR-$(date +%Y%m%d%H%M)
      echo " mv $API2ROOT/$SERVER_NAME/$SERVER_JAR $API2ROOT/$SERVER_NAME/$SERVER_JAR-$(date +%Y%m%d%H%M)"
      cp $serverJar "$API2ROOT"/$SERVER_NAME/$SERVER_JAR
      echo " cp $serverJar $API2ROOT/$SERVER_NAME/$SERVER_JAR "
      runcrm
    fi
}

function main(){
    # choice run options
    while :; do echo
      echo 'Please select the run cmd:'
      echo -e "\t${CMSG}1${CEND}. 重启$SERVER_NAME"
      echo -e "\t${CMSG}2${CEND}. 更新替换并重启$SERVER_NAME"
      read -r -e -p "Please input a number:(Default 1 press Enter) " cmd
        cmd=${cmd:-1}
        if [[ ! ${cmd} =~ ^[1-2]$ ]]; then
          echo "${CWARNING}input error! Please only input number 1~2${CEND}"
        else
          case $cmd in
                 1)  killProcess
                     if [ $? = 0 ]; then
                        echo ' start run'
			runcrm
                     fi 
                 ;;
                 2)  killProcess
                     if [ $? = 0 ]; then
                     	echo '替换jar '
			replaceJar 
                     fi
                 ;;
          esac
          break
        fi
    done
}

main

 
